package com.xframe.xdal.core.model.mapping;

import com.xframe.xdal.core.annotation.AutoFill;
import com.xframe.xdal.core.annotation.XColumn;
import com.xframe.xdal.core.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述:ITableInfo接口实现
 *
 * @author xufeng
 * @version 1.0
 * @date 2023-03-20
 */
public class TableInfo implements ITableInfo {


    /**
     * 表名称
     */
    private String name;

    /**
     * 类名称
     */
    private String code;

    /**
     * 表别名
     */
    private String asName;

    /**
     * 表描述
     */
    private String label;

    /**
     * 索引名称
     *
     * @return 索引名称
     */
    private String indexName;

    /**
     * 数据源编码
     */
    private String dataSourceDefCode;

    /**
     * 列集合
     */
    private List<IColumnMapping> columnMappingList;

    /**
     * 引用列的集合
     */
    private List<IRelation> belongsToMappingList;
    /**
     * 子表的集合
     */
    private List<IRelation> hasManyMappingList;


    @Override
    public String getIndexName() {
        if(StringUtil.isEmpty(this.indexName)){
            this.indexName = this.asName.toLowerCase();
        }
        return indexName;
    }

    @Override
    public String getDataSourceDefCode() {
        return dataSourceDefCode;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getAsName(){ return  asName; }

    @Override
    public String getLabel() {
        return this.label;
    }

    @Override
    public List<IColumnMapping> getColumnMappingList() {
        return columnMappingList;
    }

    @Override
    public void setColumnMappingList(List<IColumnMapping> columnMappingList){
        this.columnMappingList = columnMappingList;
    }

    public TableInfo(String name,String code,String asName,String label,String indexName,String dataSourceDefCode) {
        this.asName = asName;
        this.name = name;
        this.code = code;
        this.dataSourceDefCode = dataSourceDefCode;
        this.columnMappingList = new ArrayList<>();
        this.belongsToMappingList = new ArrayList<>();
        this.hasManyMappingList = new ArrayList<>();
        this.label = label;
        if(!StringUtil.isEmpty(indexName)) {
            this.indexName = indexName.toLowerCase();
        }
    }




    @Override
    public List<IRelation> getBelongsToMappingList(){
        return this.belongsToMappingList;
    }



    @Override
    public List<IRelation> getHasManyMappingList() {
        return this.hasManyMappingList;
    }


    /**
     * 查找一个IColumnMapping
     *
     * @param fieldName 属性名称
     * @return IColumnMapping
     */
    @Override
    public IColumnMapping findOneByFieldName(String fieldName) {
        IColumnMapping columnMapping = null;
        for (int index = 0; index < columnMappingList.size(); index++) {
            if (columnMappingList.get(index).getFieldName().equals(fieldName)) {
                columnMapping = columnMappingList.get(index);
                break;
            }
        }
        return columnMapping;
    }

    /**
     * 查找一个IColumnMapping
     *
     * @param name 字段名称
     * @return IColumnMapping
     */
    @Override
    public IColumnMapping findOneByName(String name) {
        IColumnMapping columnMapping = null;
        for (int index = 0; index < columnMappingList.size(); index++) {
            if (columnMappingList.get(index).getName().equals(name)) {
                columnMapping = columnMappingList.get(index);
                break;
            }
        }
        return columnMapping;
    }


    /**
     * 获取第一个数据表 主键列映射
     *
     * @return IColumnMapping
     */
    @Override
    public IColumnMapping findPkFirst() {
        IColumnMapping columnMapping = null;
        for (int index = 0; index < columnMappingList.size(); index++) {
            if (columnMappingList.get(index).getPrimaryKeyFlag()) {
                columnMapping = columnMappingList.get(index);
                break;
            }
        }
        return columnMapping;
    }

    /**
     * 获取第一个数据表 版本号列映射
     *
     * @return IColumnMapping
     */
    @Override
    public IColumnMapping findVer() {
        IColumnMapping columnMapping = null;
        for (int index = 0; index < columnMappingList.size(); index++) {
            if (columnMappingList.get(index).getVersionFlag()) {
                columnMapping = columnMappingList.get(index);
                break;
            }
        }
        return columnMapping;
    }

    /**
     * 获取所有数据表 不重复值列映射集合
     *
     * @return List<IColumnMapping>
     */
    @Override
    public List<IColumnMapping> findUniqueKeyList() {
        List<IColumnMapping> findList = new ArrayList<>();
        for (int index = 0; index < columnMappingList.size(); index++) {
            // 取不同的判断条件
            if (columnMappingList.get(index).getUniqueFlag()) {
                findList.add(columnMappingList.get(index));
            }

        }
        return findList;
    }


    /**
     * 获取数据表所有可以更新的列映射集合
     *
     * @return List<IColumnMapping>
     */
    @Override
    public List<IColumnMapping> findCanUpdateList() {
        List<IColumnMapping> findList = new ArrayList<>();
        for (int index = 0; index < columnMappingList.size(); index++) {
            IColumnMapping columnMapping = columnMappingList.get(index);
            if (!columnMapping.getPrimaryKeyFlag() && !columnMapping.getUniqueFlag() && !columnMapping.getReadonly() && !columnMapping.getVersionFlag()) {
                findList.add(columnMappingList.get(index));
            }
        }
        return findList;
    }
}
